package com.cib.ccc.poc.data.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.cib.ccc.poc.data.bean.AnalysisObject;

public class AnalysisObjectDAO {

	private Session session = null;

	public AnalysisObjectDAO() {
		Configuration config = new Configuration().configure();
		SessionFactory factory = config.buildSessionFactory();
		this.session = factory.openSession();
	}

	
	public void singleInsert(AnalysisObject obj) {
		Transaction tx = session.beginTransaction();
		session.save(obj);
		tx.commit();
		session.close();
	}

	public void buldInsert(List<AnalysisObject> list) {
		Transaction tx = session.beginTransaction();

		for (int i = 0; i < list.size(); i++) {
			AnalysisObject obj = list.get(i);
			session.save(obj);
			if (i % 20 == 0) {
				// 20, same as the JDBC batch size //20,与JDBC批量设置相同

				// flush a batch of inserts and release memory:
				// 将本批插入的对象立即写入数据库并释放内存
				session.flush();
				session.clear();
			}
		}
		tx.commit();
		session.close();
	}
}
